package com.jeeplus.invoice.api;

import com.jeeplus.invoice.invoice.service.YxfpInvoiceService;
import com.jeeplus.invoice.invoice.service.dto.YxfpInvoiceDTO;
import com.jeeplus.invoice.invoice.service.mapstruct.YxfpInvoiceWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "越香发票-发票管理API")
@RestController
@RequestMapping(value = "/invoice/api/invoice")
public class InvoiceAPI {

    @Autowired
    private YxfpInvoiceService yxfpInvoiceService;

    @Autowired
    private YxfpInvoiceWrapper yxfpInvoiceWrapper;

    /**
     * 根据客户ID查询客户所开的发票列表数据
     */
    @ApiOperation(value = "根据客户ID查询客户所开的发票列表数据")
    @GetMapping("getInvoiceListByCustomerId")
    public ResponseEntity<List<YxfpInvoiceDTO>> getInvoiceListByCustomerId(String customerId) {
        return ResponseEntity.ok(yxfpInvoiceService.getInvoiceListByCustomerId(customerId));
    }

    /**
     * 根据商户ID查询客户所开的发票列表数据
     */
    @ApiOperation(value = "根据商户ID查询客户所开的发票列表数据")
    @GetMapping("getInvoiceListByMerchantId")
    public ResponseEntity<List<YxfpInvoiceDTO>> getInvoiceListByMerchantId(String merchantId) {
        return ResponseEntity.ok(yxfpInvoiceService.getInvoiceListByMerchantId(merchantId));
    }

    /**
     * 根据发票ID获取发票信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据发票ID获取发票信息")
    @GetMapping("getInvoiceById")
    public ResponseEntity<YxfpInvoiceDTO> getInvoiceById(String id){
        return ResponseEntity.ok(yxfpInvoiceService.findById(id));
    }

    /**
     * 客户开具发票
     * @param yxfpInvoiceDTO
     * @return
     */
    @ApiOperation(value = "客户开具发票")
    @PostMapping("invoicing")
    public ResponseEntity<String> invoicing(@Valid @RequestBody YxfpInvoiceDTO yxfpInvoiceDTO){
        yxfpInvoiceDTO.setState("0");
        System.out.println("yxfpInvoiceDTO="+yxfpInvoiceDTO);
        System.out.println("yxfpInvoice="+yxfpInvoiceWrapper.toEntity(yxfpInvoiceDTO));
        yxfpInvoiceService.saveOrUpdate(yxfpInvoiceWrapper.toEntity(yxfpInvoiceDTO));
        return ResponseEntity.ok("客户开票成功");
    }

    /**
     * 商户提交客户所开具的发票
     * @param yxfpInvoiceDTO
     * @return
     */
    @ApiOperation(value = "商户提交发票")
    @PostMapping("submitInvoice")
    public ResponseEntity<String> submitInvoice(@Valid @RequestBody YxfpInvoiceDTO yxfpInvoiceDTO){
        //yxfpInvoiceDTO.setState("1");
        yxfpInvoiceService.saveOrUpdate(yxfpInvoiceWrapper.toEntity(yxfpInvoiceDTO));
        return ResponseEntity.ok("商户提交发票成功");
    }

    /**
     * 根据商户ID和发票状态查询发票列表数据
     * @param merchantId 商户ID
     * @param state      发票状态（0-客户提交，1-商户提交，2-完成开票）
     * @return
     */
    @ApiOperation(value = "根据商户ID和发票状态查询发票列表数据")
    @GetMapping("getInvoiceListByMerchantIdAndState")
    public ResponseEntity<List<YxfpInvoiceDTO>> getInvoiceListByMerchantIdAndState(String merchantId,String state) {
        return ResponseEntity.ok(yxfpInvoiceService.getInvoiceListByMerchantIdAndState(merchantId,state));
    }

    /**
     * 根据客户ID和商户名称查询发票列表数据
     * @param customerId
     * @param merchantName
     * @return
     */
    @ApiOperation(value = "根据客户ID和商户名称查询发票列表数据")
    @GetMapping("getInvoiceListByCustomerIdAndMerchantName")
    public ResponseEntity<List<YxfpInvoiceDTO>> getInvoiceListByCustomerIdAndMerchantName(String customerId,String merchantName) {
        return ResponseEntity.ok(yxfpInvoiceService.getInvoiceListByCustomerIdAndMerchantName(customerId,"%"+merchantName+"%"));
    }


}
